package bishiqiangxun;
import java.util.*;
public class Test3 {
    //题目 : 笔试强训 day37 第三题
    //算法 : dfs  记忆化搜索
    public static int[][] arr;
    public static int[][] flg;
    public static int[] dx = {1,-1,0,0};
    public static int[] dy = {0,0,1,-1};
    public static int n,m;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        //记忆化搜索  dfs
        //先输入
        n = in.nextInt();
        m = in.nextInt();
        arr = new int[n+20][m+20];
        flg = new int[n+20][m+20];

        for(int i = 1; i <= n; i++) {
            for(int j = 1;j <= m; j++) {
                arr[i][j] = in.nextInt();
            }
        }

        int count = 0;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j<= m; j++) {
                count = Math.max(count,dfs(i,j));
            }
        }
        System.out.println(count);
    }

    public static int dfs(int x, int y){
        if(flg[x][y] != 0) {
            return flg[x][y];
        }
        flg[x][y] = 1;
        for(int k = 0; k < 4; k++) {
            int x1 = x + dx[k];
            int y1 = y + dy[k];
            if(x1 >= 1&& x1 <= n && y1 >= 1&& y1 <= m&& arr[x1][y1] < arr[x][y]) {
                flg[x][y] = Math.max(flg[x][y],dfs(x1,y1)+1);
            }
        }
        return flg[x][y];
    }
}
